package com.nit.lab.mapper.basic;

import cn.hutool.core.date.DateTime;
import com.nit.lab.dto.basic.GoodsDTO;
import com.nit.lab.entity.basic.GoodsEntity;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface GoodsMapper {

    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: wjm
     * @Date: 2022/06/28 14:23
     */
    @Select({"<script>SELECT " +
            "goods_id AS `key`, goods_id, " +
            "token_id, creator, owner, goods_name, goods_desc, " +
            "photo_url, t2.user_name creator2, t3.user_name owner2 " +
            "FROM basic_goods t1 " +
            "LEFT JOIN basic_user t2 ON t1.creator = t2.user_info " +
            "LEFT JOIN basic_user t3 ON t1.owner = t3.user_info " +
            "WHERE TRUE " +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "ORDER BY " +
            "goods_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})

    List<GoodsDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("goodsName") String goodsName
            ) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_goods " +
            "WHERE TRUE " +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("goodsName") String goodsName
            ) throws Exception;

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Insert("INSERT INTO basic_goods (" +
            "token_id, goods_name, goods_desc, photo_url, owner, creator, status, create_time )" +
            "VALUES (#{GoodsEntity.tokenId}, #{GoodsEntity.goodsName}, #{GoodsEntity.goodsDesc}, " +
            "#{GoodsEntity.photoUrl}, #{GoodsEntity.owner},  #{GoodsEntity.creator}, #{GoodsEntity.status}, #{GoodsEntity.createTime} )")
    @Options(useGeneratedKeys = true, keyProperty = "goodsId", keyColumn = "goodsId")
    Integer add(@Param("GoodsEntity") GoodsEntity goodsEntity) throws Exception;

    /**
     * @description: 更新
     * @Author: WJM
     * @Date: 2022/7/5 0:03
     **/
    @Update("UPDATE basic_goods set " +
            "token_id = #{GoodsEntity.tokenId}, " +
            "goods_name = #{GoodsEntity.goodsName}, " +
            "goods_desc = #{GoodsEntity.goodsDesc}, " +
            "photo_url = #{GoodsEntity.photoUrl}, " +
            "creator = #{GoodsEntity.creator}, " +
            "owner = #{GoodsEntity.owner} " +
            "WHERE goods_id = #{GoodsEntity.goodsId}")
    Integer update(@Param("GoodsEntity") GoodsEntity goodsEntity) throws Exception;

    @Update("UPDATE basic_goods set " +
            "status = #{status} " +
            "WHERE list_id = #{listId} AND goods_number = #{goodsNumber}")
    void updateStatus(@Param("listId") Integer listId, @Param("goodsNumber") Integer goodsNumber, @Param("status") Integer status) throws Exception;

    @Update("UPDATE basic_goods set " +
            "token_id = #{GoodsEntity.tokenId}, " +
            "goods_name = #{GoodsEntity.goodsName}, " +
            "goods_desc = #{GoodsEntity.goodsDesc}, " +
            "photo_url = #{GoodsEntity.photoUrl}, " +
            "creator = #{GoodsEntity.creator}, " +
            "owner = #{GoodsEntity.owner}, " +
            "status = #{GoodsEntity.status}, " +
            "modify_time = #{GoodsEntity.modifyTime} " +
            "WHERE goods_id = #{GoodsEntity.goodsId} ")
    Integer update2(@Param("GoodsEntity") GoodsEntity goodsEntity) throws Exception;

    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Delete("DELETE FROM basic_goods WHERE goods_id = #{goodsId}")
    Integer remove(@Param("goodsId") Long goodsId) throws Exception;

    /**
     * @description:
     * @Author: WJM
     * @Date: 2022/7/13 20:09
     **/
    @Insert("INSERT INTO history_goods (" +
            "token_id, goods_name, goods_desc, photo_url, owner, creator )" +
            "VALUES (#{GoodsEntity.tokenId}, #{GoodsEntity.goodsName}, #{GoodsEntity.goodsDesc}, " +
            "#{GoodsEntity.photoUrl}, #{GoodsEntity.owner},  #{GoodsEntity.creator})")
    @Options(useGeneratedKeys = true, keyProperty = "goodsId", keyColumn = "goodsId")
    Integer addHistory(@Param("GoodsEntity") GoodsEntity goodsEntity) throws Exception;

    /**
     * @description: 通过商品种类列表id去找某件商品的信息
     * @Author: WJM
     * @Date: 2022/7/16 20:43
     **/
    @Select("SELECT * FROM basic_goods " +
            "WHERE list_id = #{listId} AND goods_number = #{goodsNumber} ")
    GoodsEntity getInfo(@Param("listId") Integer listId, @Param("goodsNumber") Integer goodsNumber) throws Exception;


    @Select("SELECT * FROM basic_goods " +
            "WHERE goods_id = #{goodsId} ")
    GoodsEntity getById(@Param("goodsId") Integer goodsId) throws Exception;
    /**
     * @description: 通过userId或拥有者获取goods信息
     * @Author: SKW
     * @Date: 2022/7/12 1:46
     **/
    @Select("SELECT * FROM basic_goods " +
            "WHERE owner = #{owner} ")
    List<GoodsEntity> getByUser(@Param("owner") String owner) throws Exception;

    @Select("SELECT status FROM basic_goods " +
            "WHERE list_id = #{listId} AND goods_number = #{goodsNumber} ")
    Integer getStatusById(@Param("listId") Integer listId, @Param("goodsNumber") Integer goodsNumber) throws Exception;

    /**
     * @description: 通过list_id获取对应的商品信息
     * @Author: SKW
     * @Date: 2022/7/12 1:46
     **/
    @Select({"<script>SELECT " +
            "goods_id AS `key`, goods_id, list_id, goods_number, " +
            "token_id, creator, owner, goods_name, goods_desc, " +
            "photo_url, t2.user_name creator2, t3.user_name owner2 " +
            "FROM basic_goods t1 " +
            "LEFT JOIN basic_user t2 ON t1.creator = t2.user_info " +
            "LEFT JOIN basic_user t3 ON t1.owner = t3.user_info " +
            "WHERE  list_id = #{listId} " +
            "ORDER BY " +
            "goods_number " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<GoodsDTO> list4TableByListId(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("listId") Long listId
    ) throws Exception;

    /**
     * @Description: 获取根据listId获取的表格数据记录的总条数
     * @Author: SKW
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_goods " +
            "WHERE  list_id = #{listId}" +
            "</script>"})
    Long count4TableByListId(
            @Param("listId") Long listId
    ) throws Exception;

    @Insert("<script>INSERT INTO basic_goods (" +
            "token_id, goods_name, goods_desc, photo_url, owner, creator, " +
            "status, list_id, goods_number )" +
            "VALUES " +
            "<foreach collection=\"entities\" index=\"index\" item=\"GoodsEntity\" separator=\",\">  " +
            "(#{GoodsEntity.tokenId}, #{GoodsEntity.goodsName}, #{GoodsEntity.goodsDesc}, " +
            "#{GoodsEntity.photoUrl}, #{GoodsEntity.owner},  #{GoodsEntity.creator}," +
            " #{GoodsEntity.status}, #{GoodsEntity.listId}, " +
            "#{GoodsEntity.goodsNumber} )" +
            "</foreach>" +
            "</script>"
            )
    Integer addAll(@Param("entities") List<GoodsEntity> goodsEntities) throws Exception;




    // temp
    @Select(("SELECT * FROM basic_goods WHERE goods_id = #{goodsId}"))
    List<GoodsEntity> all(@Param("goodsId") Integer goodsId) throws Exception;

    @Update("UPDATE basic_goods set " +
            "token_id = #{GoodsEntity.tokenId}, " +
            "goods_name = #{GoodsEntity.goodsName}, " +
            "goods_desc = #{GoodsEntity.goodsDesc}, " +
            "photo_url = #{GoodsEntity.photoUrl}, " +
            "creator = #{GoodsEntity.creator}, " +
            "owner = #{GoodsEntity.owner}, " +
            "hash_id = #{GoodsEntity.hashId} " +
            "WHERE goods_id = #{GoodsEntity.goodsId}")
    Integer updateAll(@Param("GoodsEntity") GoodsEntity goodsEntity) throws Exception;
}
